package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Utils;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    //查询User对象
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if (user==null){
            return null;
        }
        Integer userId = user.getId();
       Set<Role> roles= roleDao.findByUserId(userId);
       if (roles!=null&&roles.size()>0){
           for (Role role : roles){
           Integer roleId = role.getId();
           Set<Permission> permissions=permissionDao.findByRoleId(roleId);
           if (permissions!=null&&permissions.size()>0){
               role.setPermissions(permissions);
           }
         }
         user.setRoles(roles);
       }
        return user;
    }
    //添加用户
    public void add(User user) {
        if (user.getPassword()!=null){
            user.setPassword(MD5Utils.md5(user.getPassword()));
        }
            userDao.add(user);
    }

    //分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        //select * from t_checkitem limit 0,10
        Page<User> page = userDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<User> rows = page.getResult();
        return new PageResult(total,rows);
    }
    //根据用户的ID查询t_user_role表和t_role表
    public List<Integer> findById(Integer id) {
        return userDao.findById(id);
    }

    public List<Role> findAllRole() {
        return userDao.findAllRole();
    }

    public void edit(Integer id, Integer[] roleIds) {
        //先解除关联
        userDao.deleteAssociation(id);
        //设置关联关系
       this.setUserAndRole(id,roleIds);
    }


    public void delete(Integer id) {
        userDao.deleteAssociation(id);
        userDao.deleteUser(id);
    }

    //设置用户和角色关联关系的方法
    public  void setUserAndRole(Integer id,Integer[]roleIds){
        if (roleIds!=null&&roleIds.length>0){
            for (Integer roleId : roleIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("userId",id);
                map.put("roleId",roleId);
                userDao.setUserAndRole(map);
            }
        }
    }
}
